#define _CRT_SECURE_NO_WARNINGS
#include "Stack.h"

void StackInit(Stack* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}

void StackPrint(Stack* ps)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->top; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}

void StackDestroy(Stack* ps)
{
	assert(ps);
	free(ps->arr);
	ps->arr = NULL; 
	ps->top = ps->capacity = 0;
}

void StackCheck(Stack* ps)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->top == 0 ? 4 : 2 * ps->capacity;
		StackType* tmp = (StackType*)realloc(ps->arr, newcapacity * sizeof(Stack));
		if (tmp == NULL)
		{
			perror("realloc fail\n");
			exit(-1);
		}
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
}

void StackPush(Stack* ps,StackType val)
{
	assert(ps);
	StackCheck(ps);
	ps->arr[ps->top] = val;
	ps->top++;
}

void StackPop(Stack* ps)
{
	assert(ps);
	if (!StackEmpty(ps))
	{
		(ps->top)--;
	}
}

StackType StackTop(Stack* ps)
{
	return ps->arr[ps->top - 1];
}

int StackSize(Stack* ps)
{
	assert(ps);

	return ps->top;
}

bool StackEmpty(Stack* ps)
{
	assert(ps);
	if (ps->top == 0)
	{
		return true;
	}
	return false;
	//return ps->top == 0;
}